package com.atguigu.gmalllogger.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/2/8 9:58
 */
@RestController
@Slf4j
public class LoggerController {
    // http://192.168.18.228:8081/applog
    
    @RequestMapping("/applog")
    public String logger(@RequestParam("param") String logStr) {
        
        // 1. 把数据落盘
        writeToDisk(logStr);
        
        // 2. 把数据直接写入到kafka中(重要)
        sendToKafka(logStr);
        
        return "ok";
    }
    
    @Autowired
    KafkaTemplate<String, String> kafka;
    
    private void sendToKafka(String logStr) {
        kafka.send("ods_log", logStr);
    }
    
    private void writeToDisk(String logStr) {
        // 数据存入到磁盘  1. 使用java io  2. 使用日志工具 log4j log4j2 logback
        log.info(logStr);
    }
}
